package com.tfxy.hxl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tfxy.hxl.R;
import com.tfxy.hxl.entity.Activity;
import com.tfxy.hxl.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    // 创建活动
    @PostMapping("/create")
    public R createActivity(@RequestBody Activity activity) {
        activity.setCreateTime(new Date());
        activity.setUpdateTime(new Date());
        activity.setStatus(1);
        activity.setCurrentParticipants(0);
        boolean saved = activityService.save(activity);
        return saved ? R.ok().message("活动创建成功") : R.error().message("活动创建失败");
    }
    // 分页查询活动列表
    @GetMapping("/list")
    public R getActivityList(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String title) {
        
        Page<Activity> page = new Page<>(current, size);
        QueryWrapper<Activity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);
        
        if (title != null && !title.trim().isEmpty()) {
            queryWrapper.like("title", title);
        }
        
        queryWrapper.orderByDesc("create_time");
        Page<Activity> result = activityService.page(page, queryWrapper);
        
        return R.ok().data("activities", result.getRecords())
                .data("total", result.getTotal())
                .data("current", result.getCurrent())
                .data("size", result.getSize());
    }

    // 根据ID查询活动详情
    @GetMapping("/{id}")
    public R getActivityById(@PathVariable Long id) {
        Activity activity = activityService.getById(id);
        return activity != null ? R.ok().data("activity", activity) : R.error().message("活动不存在");
    }

    // 更新活动
    @PutMapping("/update")
    public R updateActivity(@RequestBody Activity activity) {
        activity.setUpdateTime(new Date());
        boolean updated = activityService.updateById(activity);
        return updated ? R.ok().message("活动更新成功") : R.error().message("活动更新失败");
    }

    // 删除活动
    @DeleteMapping("/{id}")
    public R deleteActivity(@PathVariable Long id) {
        boolean removed = activityService.removeById(id);
        return removed ? R.ok().message("活动删除成功") : R.error().message("活动删除失败");
    }

    // 取消活动
    @PutMapping("/cancel/{id}")
    public R cancelActivity(@PathVariable Long id) {
        Activity activity = activityService.getById(id);
        if (activity == null) {
            return R.error().message("活动不存在");
        }
        activity.setStatus(0);
        activity.setUpdateTime(new Date());
        boolean updated = activityService.updateById(activity);
        return updated ? R.ok().message("活动取消成功") : R.error().message("活动取消失败");
    }
} 